﻿<%@ WebHandler Language="C#" Class="LoginHandler" %>

using System;
using System.Web;
using TFXK.BLL;
using TFXK.Common;

public class LoginHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private static readonly CustomsBLL bll = new CustomsBLL();
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.StatusCode = 200;
        string uname = StringUtil.HtmlEncode(context.Request.QueryString["UserName"] + "");
        string upass = StringUtil.HtmlEncode(context.Request.QueryString["UserPass"] + "");
        string checkCode = StringUtil.HtmlEncode(context.Request.QueryString["CheckCode"] + "");
        try
        {
            checkLogin(uname, upass, checkCode,context);
        }
        catch(Exception ex) {
            context.Response.Write(ex.Message);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void checkLogin(string uname, string upass, string checkCode, HttpContext context)
    {
        string ValCode = context.Request.Cookies["CheckCode"].Value + "";
        if (ValCode.ToLower().Equals(checkCode.ToLower()))
        {
            if (!string.IsNullOrEmpty(uname) && !string.IsNullOrEmpty(upass))
            {
                bool flag = bll.Exists(uname, upass);
                if (flag)
                {
                    TFXK.Model.Customs model = bll.GetModelByUserName(uname);
                    // 检查审核状态
                    if (model.userState == 1)
                    {
                        System.Web.HttpContext.Current.Session["UserLogin"] = model;
                        System.Web.HttpContext.Current.Session.Timeout = 120;
                        context.Response.Write("1");//登录成功
                    }
                    else {
                        context.Response.Write("-3");//账户异常
                    }
                }
                else {
                    context.Response.Write("-4");//用户名不存在
                }
            }
            else
            {
                context.Response.Write("-2");//登录信息不能为空
            }
        }
        else
        {
            context.Response.Write("-1");//验证码不正确
        }
    }

}